package com.agent.user.controller.admin;

import com.agent.common.result.ApiResponse;
import com.agent.common.result.PageResult;
import com.agent.user.annotation.AdminLog;
import com.agent.user.dto.KeywordPage;
import com.agent.user.entity.AdminUser;
import com.agent.user.service.AdminUserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 后台管理员用户管理
 */
@RestController
@RequestMapping("/admin/users")
@RequiredArgsConstructor
public class AdminUserController {
    private final AdminUserService adminUserService;

    /**
     * 创建管理员
     *
     * @param adminUser
     * @return
     */
    @PostMapping("/add")
    @AdminLog("创建管理员")
    public ApiResponse<AdminUser> createAdmin(@RequestBody AdminUser adminUser) {
        return ApiResponse.success(adminUserService.createAdmin(adminUser));
    }

    /**
     * 获取管理员信息
     *
     * @param id
     * @return
     */
    @GetMapping("/info")
    public ApiResponse<AdminUser> getAdminById(@RequestParam("id") Long id) {
        return ApiResponse.success(adminUserService.getAdminById(id));
    }

    /**
     * 修改管理员信息
     *
     * @param updateRequest
     * @return
     */
    @PutMapping("/update")
    @AdminLog("更新管理员")
    public ApiResponse<AdminUser> updateAdmin(@RequestBody AdminUser updateRequest) {
        return ApiResponse.success(adminUserService.updateAdmin(updateRequest));
    }

    /**
     * 删除管理员信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    @AdminLog("删除管理员")
    public ApiResponse<Void> deleteAdmin(@RequestParam("id") Long id) {
        adminUserService.deleteAdmin(id);
        return ApiResponse.success();
    }

    /**
     * 管理员信息分页列表
     *
     * @param page
     * @return
     */
    @GetMapping("/search/paging")
    public PageResult<AdminUser> searchAdmins(KeywordPage page) {
        IPage<AdminUser> admins = adminUserService.searchAdmins(page);
        return PageResult.success(admins);
    }
}